<?php

namespace api\zwt\middleware;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class CheckToken
{
    public function handle($request, \Closure $next)
    {
        //halt('123');
        $header = request()->header();
        if(!isset($header['token'])||empty($header['token']))
        {
            return json([[
                'code' => 0,
                'msg' => '缺少token'
            ]]);
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        $res = JWT::decode($header['token'],$key);
        try {
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception) {
            return json([[
                'code' => 0,
                'msg' => $exception->getMessage()
            ]]);
        }
    $request->user_id = $res->data->id;
        return $next($request);
    }
}
